package com.sky.mapper;


import com.github.pagehelper.Page;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.entity.OrderDetail;
import com.sky.entity.Orders;
import com.sky.result.PageResult;
import com.sky.vo.OrderStatisticsVO;
import com.sky.vo.OrderVO;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Mapper
public interface OrderMapper {


    /**
     * 订单表插入数据
     *
     * @param orders
     * @return
     */
    void insert(Orders orders);

    /**
     * 根据订单号和用户id查询订单
     *
     * @param orderNumber
     * @param userId
     */
    @Select("select * from orders where number = #{orderNumber} and user_id= #{userId}")
    Orders getByNumberAndUserId(String orderNumber, Long userId);

    /**
     * 修改订单信息
     *
     * @param orders
     */
    void update(Orders orders);

    /**
     * 分页查询历史订单
     *
     * @param ordersPageQueryDTO
     * @return
     */
    Page<Orders> queryHistoricalOrders(OrdersPageQueryDTO ordersPageQueryDTO);


    @Select("select id from  orders where user_id = #{userId}")
    List<Long> getOrderId(Long userId);


    @Select("select  * from orders where  id = #{id}")
    Orders getDetails(Long orderId);

    /**
     * 取消订单
     *
     * @param id
     */
    @Update("update orders set status = '6' where id = #{id}")
    void cancelOrder(Long id);

    /**
     * 再来一单
     *
     * @param orderDetails
     */
    void oneMoreOrder(List<OrderDetail> orderDetails);

    /**
     * 根据id查询订单
     *
     * @param id
     * @return
     */
    @Select("select * from orders where  id = #{id}")
    Orders getById(Long id);

    /**
     * 分页查询订单
     *
     * @param ordersPageQueryDTO
     * @return
     */
    Page<Orders> pagingQuery(OrdersPageQueryDTO ordersPageQueryDTO);

    /**
     * 统计订单数量
     *
     * @return
     */
    List<Integer> statisticalQuantity();

    /**
     * 获取各种状态订单数量
     *
     * @param status
     * @return
     */
    @Select("select count(*) from orders where status = #{status}")
    Integer countStatus(Integer status);


    /**
     * 获取状态
     *
     * @param id
     * @return
     */
    @Select("select status from orders where id = #{id}")
    Integer getStatus(Long id);

    /**
     * 拒单
     *
     * @param orders
     */
    @Update("update orders set status =#{status},cancel_time = #{cancelTime}, rejection_reason = #{rejectionReason} where id =#{id}")
    void rejection(Orders orders);

    /**
     * 接单
     *
     * @param orders
     */
    @Update("update orders set status = #{status} where id = #{id}")
    void takeOrders(Orders orders);

    /**
     * 取消订单
     *
     * @param orders
     */
    @Update("update orders set status =#{status},cancel_time = #{cancelTime}, cancel_reason = #{cancelReason} where id =#{id}")
    void cancel(Orders orders);

    /***
     * 处理超时订单
     * @param status
     * @param time
     * @return
     */
    @Select("select  * from orders where status = #{status} and order_time < #{time}")
    List<Orders> processingTimeoutOrders(Integer status, LocalDateTime time);


    Integer countByMap(Map map);


    Double sumByMap(Map map);
}
